Ext.define('YX.core.ScheduleJob', {
	extend : 'Ext.panel.Panel',
	layout : {
		type : 'vbox',
		align : 'stretch'
	},
	frmJob : null,
	cmbOriJobDetailId : null,
	cmbJobClass : null,
	txtJobDetailDesc : null,
	txtJobData : null,
	btnSaveJob : null,
	btnDeleteJob : null,
	frmOper : null,
	txtTaskName : null,
	txtTaskDesc : null,
	cmbJobDetailId : null,
	fcFromDate : null,
	dfFromDate : null,
	tfFromTime : null,
	fcToDate : null,
	dfToDate : null,
	tfToTime : null,
	txtCron : null,
	btnSave : null,
	btnStart : null,
	btnPause : null,
	btnDelete : null,
	btnReset : null,
	gridTask : null,
	initComponent : function() {
		this.cmbOriJobDetailId = Ext.create('Ext.form.field.ComboBox', {
			editable : true,
			allowBlank : false,
			maxLength : 30,
			flex : 1,
			queryMode : 'local',
			fieldLabel : T.label.jobDetailId,
			valueField : 'jobName',
			displayField : 'jobName',
			store : Ext.create('Ext.data.JsonStore', {
				autoDestory : true,
				autoLoad : true,
				fields : [ 'jobName', 'jobDesc', 'jobClass', 'jobData', 'status' ],
				proxy : {
					type : 'zbusrpc',
					module : 'BasViewJobDetailListService',
					reader : {
						root : 'jobList'
					}
				}
			}),
			listeners : {
				scope : this,
				blur : this.onCmbOriJobDetailIdBlur
			}
		});

		this.txtJobDetailDesc = Ext.create('Ext.form.field.Text', {
			fieldLabel : T.label.desc,
			maxLength : 100,
			flex : 1
		});

		this.cmbJobClass = Ext.create('Ext.form.field.ComboBox', {
			editable : false,
			allowBlank : false,
			queryMode : 'local',
			width : 150,
			fieldLabel : T.label.jobClass,
			valueField : 'text',
			displayField : 'text',
			store : Ext.create('Ext.data.Store', {
				autoDestory : true,
				fields : [ 'text' ],
				data : [ {
					text : 'org.yx.job.SystemQuartzJobBean'
				} ]
			})
		});

		this.txtJobData = Ext.create('Ext.form.field.TextArea', {
			fieldLabel : T.label.desc,
			height : 100,
			maxLength : 500,
			anchor : '100%',
			grow : true,
			emptyText : '{"serviceName":"BasViewUserService","orgCode":"FACT","birthDate":"19900101010100","userIdList":[{"userId":"123"},{"userId":"234"}]}'
		});

		this.btnSaveJob = Ext.create('Ext.button.Button', {
			text : T.label.save,
			itemId : 'btnSave',
			scope : this,
			handler : this.onBtnSaveJobClicked
		});

		this.btnDeleteJob = Ext.create('Ext.button.Button', {
			text : T.label.del,
			itemId : 'btnDelete',
			scope : this,
			handler : this.onBtnDeleteJobClicked
		});

		this.frmJob = Ext.create('Ext.form.Panel', {
			title : T.label.job,
			tbar : [ this.btnSaveJob, '-', this.btnDeleteJob ],
			items : [ {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				defaults : {
					flex : 1
				},
				items : [ this.cmbOriJobDetailId, this.txtJobDetailDesc, this.cmbJobClass ]
			}, this.txtJobData ]
		});

		this.txtTaskName = Ext.create('Ext.form.field.Text', {
			allowBlank : false,
			name : 'jobName',
			fieldLabel : T.label.triggerName
		});

		this.txtTaskDesc = Ext.create('Ext.form.field.Text', {
			fieldLabel : T.label.desc,
			name : 'jobDesc'
		});

		this.cmbJobDetailId = Ext.create('Ext.form.field.ComboBox', {
			editable : false,
			allowBlank : false,
			name : 'jobDetailId',
			queryMode : 'local',
			fieldLabel : T.label.jobDetailId,
			valueField : 'jobName',
			displayField : 'jobName',
			store : this.cmbOriJobDetailId.getStore()
		});

		this.dfFromDate = Ext.create('Ext.form.field.Date', {
			editable : false,
			flex : 1,
			format : 'Y-m-d'
		});

		this.tfFromTime = Ext.create('Ext.form.field.Time', {
			increment : 30,
			flex : 1,
			format : 'H:i'
		});

		this.fcFromDate = Ext.create('Ext.form.FieldContainer', {
			layout : 'hbox',
			fieldLabel : T.label.fromDate,
			items : [ this.dfFromDate, this.tfFromTime ]
		});

		this.dfToDate = Ext.create('Ext.form.field.Date', {
			editable : false,
			flex : 1,
			format : 'Y-m-d'
		});

		this.tfToTime = Ext.create('Ext.form.field.Time', {
			increment : 30,
			flex : 1,
			format : 'H:i'
		});

		this.fcToDate = Ext.create('Ext.form.FieldContainer', {
			layout : 'hbox',
			fieldLabel : T.label.toDate,
			items : [ this.dfToDate, this.tfToTime ]
		});

		this.txtCron = Ext.create('Ext.form.field.Text', {
			allowBlank : false,
			fieldLabel : T.label.cronExpression
		});

		this.btnSave = Ext.create('Ext.button.Button', {
			text : T.label.save,
			itemId : 'btnSave',
			scope : this,
			handler : this.onBtnSaveClicked
		});

		this.btnDelete = Ext.create('Ext.button.Button', {
			text : T.label.del,
			itemId : 'btnDelete',
			scope : this,
			handler : this.onBtnDeleteClicked
		});
		this.btnStart = Ext.create('Ext.button.Button', {
			text : T.label.start,
			itemId : 'btnStart',
			scope : this,
			handler : this.onBtnStartClicked
		});
		this.btnPause = Ext.create('Ext.button.Button', {
			text : T.label.pause,
			itemId : 'btnPause',
			scope : this,
			handler : this.onBtnPauseClicked
		});
		this.btnReset = Ext.create('Ext.button.Button', {
			text : T.label.reset,
			itemId : 'btnReset',
			scope : this,
			handler : this.onBtnResetClicked
		});

		this.frmOper = Ext.create('Ext.form.Panel', {
			title : T.label.trigger,
			tbar : [ this.btnSave, this.btnDelete, '-', this.btnStart, this.btnPause, this.btnReset ],
			items : [ {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				defaults : {
					flex : 1
				},
				items : [ this.txtTaskName, this.txtTaskDesc, this.cmbJobDetailId ]
			}, {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				defaults : {
					flex : 1
				},
				items : [ this.fcFromDate, this.fcToDate, this.txtCron ]
			} ]
		});

		var taskStore = Ext.create('Ext.data.JsonStore', {
			autoDestory : true,
			autoLoad : true,
			fields : [ 'jobName', 'jobDesc', 'jobDetailId', {
				name : 'fromDate',
				type : 'date',
				dateFormat : 'YmdHis'
			}, {
				name : 'toDate',
				type : 'date',
				dateFormat : 'YmdHis'
			}, {
				name : 'previousFireTime',
				type : 'date',
				dateFormat : 'YmdHis'
			}, {
				name : 'nextFireTime',
				type : 'date',
				dateFormat : 'YmdHis'
			}, 'cron', 'status' ],
			proxy : {
				type : 'zbusrpc',
				module : 'BasViewScheduleJobService',
				reader : {
					root : 'jobList'
				}
			}
		});

		this.gridTask = Ext.create('Ext.grid.Panel', {
			title : T.label.triggerList,
			flex : 1,
			columns : [ {
				text : T.label.status,
				dataIndex : 'status',
				width : 80
			}, {
				text : T.label.taskName,
				dataIndex : 'jobName',
				width : 150
			}, {
				text : T.label.desc,
				dataIndex : 'jobDesc',
				width : 200
			}, {
				text : T.label.jobDetailId,
				dataIndex : 'jobDetailId',
				width : 150
			}, {
				text : T.label.fromDate,
				dataIndex : 'fromDate',
				xtype : 'datecolumn',
				format : 'Y-m-d H:i',
				width : 150
			}, {
				text : T.label.toDate,
				dataIndex : 'toDate',
				xtype : 'datecolumn',
				format : 'Y-m-d H:i',
				width : 150
			}, {
				text : T.label.cronExpression,
				dataIndex : 'cron',
				width : 150
			}, {
				text : T.label.previousFireTime,
				dataIndex : 'previousFireTime',
				xtype : 'datecolumn',
				format : 'Y-m-d H:i:s',
				width : 150
			}, {
				text : T.label.nextFireTime,
				dataIndex : 'nextFireTime',
				xtype : 'datecolumn',
				format : 'Y-m-d H:i:s',
				width : 150
			} ],
			store : taskStore,
			bbar : {
				xtype : 'pagingtoolbar',
				store : taskStore
			},
			listeners : {
				scope : this,
				itemclick : this.onGridTaskItemClicked
			}
		});

		this.items = [ this.frmJob, this.frmOper, this.gridTask ];

		this.callParent();
	},
	afterRender : function() {
		// this.loadJobDetail();

		this.callParent();
	},
	onCmbOriJobDetailIdBlur : function(cmb) {
		var value = cmb.getValue();
		if (value == '') {
			this.frmJob.getForm().reset();
		} else {
			var index = cmb.getStore().findExact('jobName', value);
			if (index >= 0) {
				var rcd = cmb.getStore().getAt(index);
				this.txtJobDetailDesc.setValue(rcd.get('jobDesc'));
				this.cmbJobClass.setValue(rcd.get('jobClass'));
				this.txtJobData.setValue(rcd.get('jobData'));
			} else {
				// this.frmJob.getForm().reset();
			}
		}
	},
	loadJobDetail : function() {
		var dtlStore = this.cmbJobDetailId.getStore();
		if (dtlStore.getCount() == 0) {
			YX.ZbusRpc.request({
				module : 'BasViewJobDetailListService',
				success : function(rsp) {
					Ext.each(rsp.jobDetails, function(item) {
						dtlStore.add({
							text : item
						});
					});
				}
			});
		}
	},
	onBtnSaveJobClicked : function(btn) {
		if (!this.frmJob.isValid()) {
			return;
		}

		var me = this;

		var data = {
			jobDetailId : this.cmbOriJobDetailId.getValue(),
			jobDetailDesc : this.txtJobDetailDesc.getValue(),
			jobClass : this.cmbJobClass.getValue(),
			status : 'J',
			jobDetailStatus : 'N',
			jobData : this.txtJobData.getValue()
		};

		try {
			data.jsonData = Ext.JSON.encode(Ext.JSON.decode(data.jsonData));
			this.txtJobData.clearInvalid();
		} catch (ex) {
			this.txtJobData.markInvalid(ex.message);
			return;
		}

		Ext.Msg.show({
			title : T.label.confirm,
			msg : T.msg.are_you_sure_to_save_data,
			icon : Ext.Msg.QUESTION,
			buttons : Ext.Msg.YESNO,
			fn : function(btnId) {
				if ('yes' == btnId) {
					btn.disable();

					YX.ZbusRpc.request({
						module : 'BasSaveScheduleJobService',
						scope : this,
						showSuccess : true,
						params : data,
						callback : function() {
							btn.enable();
						},
						success : function() {
							me.cmbOriJobDetailId.getStore().reload();
						}
					});
				}
			}
		});
	},
	onBtnDeleteJobClicked : function(btn) {
		if (!this.frmJob.isValid()) {
			return;
		}

		var me = this;

		var data = {
			jobDetailId : this.cmbOriJobDetailId.getValue(),
			jobDetailDesc : this.txtJobDetailDesc.getValue(),
			jobClass : this.cmbJobClass.getValue(),
			status : 'J',
			jobDetailStatus : 'D',
			jobData : this.txtJobData.getValue()
		};

		try {
			data.jsonData = Ext.JSON.encode(Ext.JSON.decode(data.jsonData));
			this.txtJobData.clearInvalid();
		} catch (ex) {
			this.txtJobData.markInvalid(ex.message);
			return;
		}

		Ext.Msg.show({
			title : T.label.del,
			msg : T.msg.are_you_sure_to_delete_data,
			icon : Ext.Msg.QUESTION,
			buttons : Ext.Msg.YESNO,
			fn : function(btnId) {
				if ('yes' == btnId) {
					btn.disable();

					YX.ZbusRpc.request({
						module : 'BasSaveScheduleJobService',
						scope : this,
						showSuccess : true,
						params : data,
						callback : function() {
							btn.enable();
						},
						success : function() {
							me.cmbOriJobDetailId.getStore().reload();
						}
					});
				}
			}
		});
	},
	onBtnSaveClicked : function(btn) {
		if (!this.frmOper.isValid()) {
			return;
		}

		var dtFrom = null;
		if (this.dfFromDate.getValue()) {
			dtFrom = this.dfFromDate.getValue();
			dtFrom.setHours(0);
			dtFrom.setMinutes(0);
			dtFrom.setSeconds(0);
			if (this.tfFromTime.getValue()) {
				var tm = this.tfFromTime.getValue();
				dtFrom.setHours(tm.getHours());
				dtFrom.setMinutes(tm.getMinutes());
				dtFrom.setSeconds(tm.getSeconds());
			}
		}
		var dtTo = null;
		if (this.dfToDate.getValue()) {
			dtTo = this.dfToDate.getValue();
			dtTo.setHours(0);
			dtTo.setMinutes(0);
			dtTo.setSeconds(0);
			if (this.tfToTime.getValue()) {
				var tm = this.tfToTime.getValue();
				dtTo.setHours(tm.getHours());
				dtTo.setMinutes(tm.getMinutes());
				dtTo.setSeconds(tm.getSeconds());
			}
		}

		if (dtFrom.getTime() >= dtTo.getTime()) {
			this.dfToDate.markInvalid('Invalid date.');
			return;
		} else {
			this.dfToDate.clearInvalid();
		}

		var data = {
			jobName : this.txtTaskName.getValue(),
			jobDesc : this.txtTaskDesc.getValue(),
			jobDetailId : this.cmbJobDetailId.getValue(),
			fromDate : dtFrom,
			toDate : dtTo,
			cron : this.txtCron.getValue()
		};

		btn.disable();
		YX.ZbusRpc.request({
			module : 'BasSaveScheduleJobService',
			scope : this,
			showSuccess : true,
			params : data,
			callback : function() {
				btn.enable();
			},
			success : function() {
				this.gridTask.getStore().reload();
			}
		});
	},
	onBtnDeleteClicked : function(btn) {
		if (!this.txtTaskName.isValid()) {
			return;
		}

		btn.disable();
		YX.ZbusRpc.request({
			module : 'BasSaveScheduleJobService',
			scope : this,
			showSuccess : true,
			params : {
				jobName : this.txtTaskName.getValue(),
				status : 'D'
			},
			callback : function() {
				btn.enable();
			},
			success : function() {
				this.gridTask.getStore().reload();
			}
		});
	},
	onBtnStartClicked : function(btn) {
		if (!this.txtTaskName.isValid() || !this.cmbJobDetailId.isValid()) {
			return;
		}

		btn.disable();
		YX.ZbusRpc.request({
			module : 'BasSaveScheduleJobService',
			scope : this,
			showSuccess : true,
			params : {
				jobName : this.txtTaskName.getValue(),
				jobDetailId : this.cmbJobDetailId.getValue(),
				status : 'S'
			},
			callback : function() {
				btn.enable();
			},
			success : function() {
				this.gridTask.getStore().reload();
			}
		});
	},
	onBtnPauseClicked : function(btn) {
		if (!this.txtTaskName.isValid() || !this.cmbJobDetailId.isValid()) {
			return;
		}

		btn.disable();
		YX.ZbusRpc.request({
			module : 'BasSaveScheduleJobService',
			scope : this,
			showSuccess : true,
			params : {
				jobName : this.txtTaskName.getValue(),
				jobDetailId : this.cmbJobDetailId.getValue(),
				status : 'P'
			},
			callback : function() {
				btn.enable();
			},
			success : function() {
				this.gridTask.getStore().reload();
			}
		});
	},
	onBtnResetClicked : function(btn) {
		this.frmOper.getForm().reset();
	},
	onGridTaskItemClicked : function(grid, record, item, index) {
		var data = record.data;
		this.txtTaskName.setValue(data.jobName);
		this.txtTaskDesc.setValue(data.jobDesc);
		this.cmbJobDetailId.setValue(data.jobDetailId);
		this.dfFromDate.setValue(data.fromDate);
		this.tfFromTime.setValue(data.fromDate);
		this.dfToDate.setValue(data.toDate);
		this.tfToTime.setValue(data.toDate);
		this.txtCron.setValue(data.cron);
	}
});